package com.mes.fixture.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.fixture.mapper.FixtureTypeMapper;
import com.mes.fixture.server.FixtureTypeServer;
import com.mes.pojo.fixture.FixtureType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/22 14:38
 */
@Service
public class FixtureTypeServerImpl implements FixtureTypeServer {
    @Autowired
    private FixtureTypeMapper fixtureTypeMapper;

    @Override
    public MESResult queryAll(FixtureType fixtureType, Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<FixtureType> fixtureTypeList = fixtureTypeMapper.selectAll(fixtureType);
        PageInfo<FixtureType> fixtureTypePageInfo = new PageInfo<>(fixtureTypeList);
        return MESResult.success(fixtureTypePageInfo);
    }

    @Override
    public MESResult queryById(String fixtureTypeId) {
        return MESResult.success(fixtureTypeMapper.selectByFixtureTypeId(fixtureTypeId));
    }

    @Override
    public MESResult queryAllColumn() {
        return MESResult.success(fixtureTypeMapper.selectAllColumn());
    }

    @Override
    public MESResult deleteFixtureType(String fixtureTypeId) {
        FixtureType fixtureType = fixtureTypeMapper.selectByFixtureTypeId(fixtureTypeId);
        fixtureType.setFixtureTypeStatus(99);
        return MESResult.success(fixtureTypeMapper.updateFixtureType(fixtureType));
    }

    @Override
    public MESResult updateFixtureType(FixtureType fixtureType) {
        FixtureType same = fixtureTypeMapper.selectByFixtureTypeName(fixtureType.getFixtureTypeName());
        if (same != null && !same.getFixtureTypeId().equals(fixtureType.getFixtureTypeId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        return MESResult.success(fixtureTypeMapper.updateFixtureType(fixtureType));
    }

    @Override
    public MESResult insertFixtureType(FixtureType fixtureType) {
        FixtureType same = fixtureTypeMapper.selectByFixtureTypeName(fixtureType.getFixtureTypeName());
        if (same != null){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        String maxFixtureTypeId = fixtureTypeMapper.selectMaxFixtureTypeId();
        String[] split = maxFixtureTypeId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num > 0 && num < 10){
            strNum = "000" + num;
        }else if (num < 100){
            strNum = "00" + num;
        }else if (num < 1000){
            strNum = "0" + num;
        }else {
            strNum = num + "";
        }
        fixtureType.setFixtureTypeId(split[0] + "_" + strNum);
        return MESResult.success(fixtureTypeMapper.insertFixtureType(fixtureType));
    }
}
